package com.chinamobile.maps.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chinamobile.maps.dao.IUsersDAO;
import com.chinamobile.maps.orm.Users;

/**
 * @ClassName:   UsersDAOImpl
 * @Description: TODO
 * @author:      frankwong
 * @date:        2012-9-25 下午11:00:42
 *
 */
public class UsersDAOImpl extends HibernateDaoSupport implements IUsersDAO {

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#addUser(com.chinamobile.maps.orm.Users)
	 */
	@Override
	public void addUser(Users user) {
		this.getHibernateTemplate().save(user);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#delUser(int)
	 */
	@Override
	public void delUser(int id) {
		delUser(getUserByID(id));
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#delUser(com.chinamobile.maps.orm.Users)
	 */
	@Override
	public void delUser(Users user) {
		this.getHibernateTemplate().delete(user);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#updateUser(com.chinamobile.maps.orm.Users)
	 */
	@Override
	public void updateUser(Users user) {
		this.getHibernateTemplate().update(user);
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#getUsers()
	 */
	@Override
	public List<Users> getUsers() {
		List list = this.getHibernateTemplate().find("from Users");
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#getUserByID(int)
	 */
	@Override
	public Users getUserByID(int id) {
		List list = this.getHibernateTemplate().find("from Users as user where user.id=?",
				new Object[] { id });

		if (list != null && list.size() > 0) {
			return (Users) list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.chinamobile.maps.dao.UsersDAO#getUserByName(java.lang.String)
	 */
	@Override
	public Users getUserByName(String username) {
		List list = this.getHibernateTemplate().find(
				"from Users as user where user.loginName=?"
				, new Object[] { username });

		if (list != null && list.size() > 0) {
			return (Users) list.get(0);
		}
		return null;
	}

	@Override
	public List<Users> getObjects(String hsql) {
		List list = this.getHibernateTemplate().find(hsql);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}
}
